package cn.gemiman.daoyi.module.cms.controller.app.blogmessage;

import cn.gemiman.daoyi.framework.apilog.core.annotation.ApiAccessLog;
import cn.gemiman.daoyi.framework.common.enums.CommonStatusEnum;
import cn.gemiman.daoyi.framework.common.enums.UserTypeEnum;
import cn.gemiman.daoyi.framework.common.pojo.CommonResult;
import cn.gemiman.daoyi.framework.common.pojo.PageParam;
import cn.gemiman.daoyi.framework.common.pojo.PageResult;
import cn.gemiman.daoyi.framework.common.util.object.BeanUtils;
import cn.gemiman.daoyi.framework.excel.core.util.ExcelUtils;
import cn.gemiman.daoyi.framework.security.core.annotations.PreAuthenticated;
import cn.gemiman.daoyi.framework.security.core.util.SecurityFrameworkUtils;
import cn.gemiman.daoyi.framework.web.core.util.WebFrameworkUtils;
import cn.gemiman.daoyi.module.cms.controller.admin.blogmessage.vo.BlogMessagePageReqVO;
import cn.gemiman.daoyi.module.cms.controller.admin.blogmessage.vo.BlogMessageRespVO;
import cn.gemiman.daoyi.module.cms.controller.admin.blogmessage.vo.BlogMessageSaveReqVO;
import cn.gemiman.daoyi.module.cms.controller.admin.tagdictionary.vo.TagDictionaryRespVO;
import cn.gemiman.daoyi.module.cms.dal.dataobject.appsite.AppSiteDO;
import cn.gemiman.daoyi.module.cms.dal.dataobject.appsitecomment.AppSiteCommentDO;
import cn.gemiman.daoyi.module.cms.dal.dataobject.appsitefootprint.AppSiteFootprintDO;
import cn.gemiman.daoyi.module.cms.dal.dataobject.blogmessage.BlogMessageDO;
import cn.gemiman.daoyi.module.cms.dal.dataobject.tagdictionary.TagDictionaryDO;
import cn.gemiman.daoyi.module.cms.service.appsite.AppSiteService;
import cn.gemiman.daoyi.module.cms.service.appsitecomment.AppSiteCommentService;
import cn.gemiman.daoyi.module.cms.service.appsitefootprint.AppSiteFootprintService;
import cn.gemiman.daoyi.module.cms.service.blogmessage.BlogMessageService;
import cn.gemiman.daoyi.module.cms.service.tagdictionary.TagDictionaryService;
import cn.hutool.core.collection.CollUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.gemiman.daoyi.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.gemiman.daoyi.framework.common.pojo.CommonResult.success;
import static cn.gemiman.daoyi.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
import static cn.gemiman.daoyi.framework.web.core.util.WebFrameworkUtils.getAppId;

@Tag(name = "用户 APP - 留言")
@RestController
@RequestMapping("/cms/blog-message")
@Validated
public class AppBlogMessageController {

    @Resource
    private AppSiteService appSiteService;
    @Resource
    private BlogMessageService blogMessageService;
    @Resource
    private TagDictionaryService tagDictionaryService;
    @Resource
    private AppSiteFootprintService appSiteFootprintService;
    @Resource
    private AppSiteCommentService appSiteCommentService;

    @PostMapping("/create")
    @Operation(summary = "创建留言")
    @PreAuthenticated
    public CommonResult<Long> createBlogMessage(@Valid @RequestBody BlogMessageSaveReqVO createReqVO) {
        String appId = WebFrameworkUtils.getAppId();
        AppSiteDO appSite = appSiteService.getAppSite(appId);
        createReqVO.setAppId(appId).setAppSiteId(appSite.getId());
        createReqVO.setUserId(SecurityFrameworkUtils.getLoginUserId()).setUserType(UserTypeEnum.MEMBER.getValue());
        return success(blogMessageService.createBlogMessage(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新留言")
    @PreAuthenticated
    public CommonResult<Boolean> updateBlogMessage(@Valid @RequestBody BlogMessageSaveReqVO updateReqVO) {
        blogMessageService.updateBlogMessage(updateReqVO, true);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除留言")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthenticated
    public CommonResult<Boolean> deleteBlogMessage(@RequestParam("id") Long id) {
        blogMessageService.deleteBlogMessage(id, true);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得留言")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<BlogMessageRespVO> getBlogMessage(@RequestParam("id") Long id) {
        BlogMessageDO blogMessage = blogMessageService.getBlogMessage(id);
        if (null == blogMessage || CommonStatusEnum.isDisable(blogMessage.getStatus())) {
            return success(null);
        }
        BlogMessageRespVO bean = BeanUtils.toBean(blogMessage, BlogMessageRespVO.class);
        appSiteFootprintService.generatorIsLike(UserTypeEnum.MEMBER.getValue(), getLoginUserId(), CollUtil.toList(bean), AppSiteFootprintDO::getMessageId);
        return success(bean);
    }

    @GetMapping("/page")
    @Operation(summary = "获得留言分页")
    public CommonResult<PageResult<BlogMessageRespVO>> getBlogMessagePage(@Valid BlogMessagePageReqVO pageReqVO) {
        pageReqVO.setAppId(getAppId());
        pageReqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
        PageResult<BlogMessageDO> pageResult = blogMessageService.getBlogMessagePage(pageReqVO);
        PageResult<BlogMessageRespVO> bean = BeanUtils.toBean(pageResult, BlogMessageRespVO.class);
        appSiteFootprintService.generatorIsLike(UserTypeEnum.MEMBER.getValue(), getLoginUserId(), bean.getList(), AppSiteFootprintDO::getMessageId);
        appSiteCommentService.generatorCommentTotal(bean.getList(), AppSiteCommentDO::getMessageId);
        return success(bean);
    }

    @GetMapping("/getHotTagList")
    @Operation(summary = "获取留言所有标签")
    public CommonResult<List<TagDictionaryRespVO>> getHotTagList() {
        List<TagDictionaryDO> list = tagDictionaryService.getHotMessageTagList(getAppId());
        return success(BeanUtils.toBean(list, TagDictionaryRespVO.class));
    }
}
